vbOKCancel VBA MsgBox Ok Cancel Buttons

VBA-MsgBox-Ok-Cancel

vbOKCancel constant of VBA MsgBox is useful to display a prompt with Ok and Cancel buttons to the user. We can use vbOKCancel  constant to replace the default message box button with OK and Cancel buttons.

vbOKCancel – VBA MsgBox with Ok Cancel Buttons

VBA MsgBox with Ok and Cancel buttons is very helpful to show the important message to the user. We can prompt the user with a message using VBA MsgBox with Ok button to get the user attention. You can ask the user to press the Ok button if they agree with given statement or cancel if they disagree.

Usage

  • Useful to run the statements based on user choice.
  • Execute if the user accepts the statement
  • You can continue the process when user clicks on Ok button
  • Skip the remaining process when the user presses the Cancel button
  • User can press the Enter Key to press the Ok button
  • Pressing the Escape (Esc) button will trigger the Cancel button

Syntax

Here is the VBA syntax of the Message Box to display with Ok Cancel button. You can pass the vbOKCancel constant for the button argument of the VBA MsgBox Function.

MsgBox (prompt, [ buttons:= vbOKCancel, ] [ title, ] [ helpfilecontext ])

You can also use VBA MsgBox Enumeration Value for vbOKCancel constant as shown below:

MsgBox (prompt, [ buttons:= 1, ] [ title, ] [ helpfilecontext ])

Here 1 is the enumeration value of the vbOKCancel message box constant

Example

Following is the Example VBA Code to display a message box with OK and Cancel buttons. The example below shows the VBA MsgBox with OK Cancel buttons using VBA constant and ENUM value.

1. vbOKCancel Constant: The following example is the VBA code using VBA constant to display a message box with Ok and Cancel buttons only.

Sub VBA_MsgBox_vbOKCancel_Buttons_Constant()
MsgBox "Your Prompt and message to display OK Cancel Message Box", vbOKCancel, "Title of the vbOKCancel Message Box"
End Sub

vbOKCancel-VBA-Constant

Here, we have provided the second argument buttons as vbOKCancel constant to display the message with Ok and Cancel buttons.

2. vbOKCancel ENUM: Here is the VBA Macro using VBA Message Box Enumeration Value for VBA Ok Cancel buttons. 1 is the Enum value of vbOKCancel MsgBox constant.

Sub VBA_MsgBox_vbOKCancel_Buttons_Enum()
MsgBox "Your Prompt and message to display OK Cancel Message Box", 1, "Title of the vbOKCancel Message Box" 
End Sub

3. Ok Cancel If: We can use Ok Cancel Message Box with If statement to execute the VBA code statements based on a condition. We can run the certain procedure when based on the user choice. The following macro checks if user clicks on Ok button and process the block of VBA code for OK button click.

Sub VBA_MsgBox_vbOKCancelIf()

If MsgBox("This Process will finish the Report Generation in 30 Minutes." & vbCr & "Do you wants to Proceed?", vbOKCancel, "Please Note!") = vbOK Then

'Statements to run when user clicks on OK button
MsgBox "You have pressed Enter Key or Clicked on Ok button"


Else
'Statements to run when user clicks on Cancel button
MsgBox "You have pressed Esc Key or Clicked on Cancel button"

End If

'Statements to run after If-Else-End Statements

End Sub

4. Ok Cancel Exit Sub (Real-time scenario): Sometimes, we display the message box with a statement. User can choose to press one of the buttons (Okay or Cancel). We can continue the remaining statements when user chooses Ok, we can skip the statements and Exit Sub if the user clicks on Cancel button.

Sub VBA_MsgBox_vbOKCancelExitSub()

If MsgBox("This Process will finish the Report Generation in 30 Minutes." & vbCr & "Do you wants to Proceed?", vbOKCancel, "Please Note!") = vbOK Then

'Statements to the Process the remaining procedure
'MsgBox "Please wait while executing the remaining Procedure"

Else
'Statements to show before Exit the Process
'MsgBox "You have skipped the Process"

Exit Sub
End If

'Statements to run after If-Else-End Statements

End Sub

5. Ok Cancel Result: As shown above, we can capture the result of the OK Cancel Message Box and Return the values to a variable. We can capture the result in a variable and write two blocks of code to run based on user decision.

Sub VBA_MsgBox_vbOKCancelResult()

Dim varMsgBoxResutlt

varMsgBoxResutlt = MsgBox("This Process will finish the Report Generation in 30 Minutes." & vbCr & "Do you wants to Proceed?", vbOKCancel, "Please Note!")

If varMsgBoxResutlt = vbOK Then
'Statements to run when user clicks on OK button
MsgBox "You have pressed Enter Key or Clicked on Ok button"


ElseIf varMsgBoxResutlt = vbCancel Then
'Statements to run when user clicks on Cancel button
MsgBox "You have pressed Esc Key or Clicked on Cancel button"


Else
'Statements to run when user closes the button using any other method
End If
End Sub

Instructions

Here are the simple step by step instructions to use and run the VBA MsgBox OK Cancel Macro. This code will display a VBA OK Cancel Dialog box to the user. You can copy entire procedure or a statement to insert into your existing procedure.

  • Open the VBA Editor by pressing Alt+F11 or from Developer Tab of the Ribbon.
  • Insert a new Code Module from Insert menu of VBE
  • Copy required VBA MsgBox OK Cancel Macro and Paste in the Module
  • Press F5 or Click on the Run Sub icons on the menu to execute the code
  • Output: It will display a Message Box with OK and Cancel Buttons

Leave a Comment